package com.example.android.commitcontent.ime.keyboard.event_handler;

import android.content.Context;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;

import com.example.android.commitcontent.ime.R;
import com.example.android.commitcontent.ime.keyboard.adapters.MoreKeysAdapter;

import java.util.List;

import com.example.android.commitcontent.ime.beans.PinYinCharacter;

public class MoreKeysHandler implements View.OnTouchListener {

    private Context mContext;
    private List<PinYinCharacter> mPinYinCharacterList;
    private OnPinYinCharacterSelectedListener mOnPinYinCharacterSelectedListener;
    private PopupWindow mPopupWindow;
    private RecyclerView mRecyclerView;
    private MoreKeysAdapter mMoreKeysAdapter;
    private float mLastX, mTouchX, mDensity;

    public MoreKeysHandler(Context context, List<PinYinCharacter> pinYinCharacterList, OnPinYinCharacterSelectedListener onPinYinCharacterSelectedListener) {
        mContext = context;
        mPinYinCharacterList = pinYinCharacterList;
        mOnPinYinCharacterSelectedListener = onPinYinCharacterSelectedListener;
        initPopupWindow();
    }

    private void initPopupWindow() {
        mDensity = mContext.getResources().getDisplayMetrics().density;
        View container = LayoutInflater.from(mContext).inflate(R.layout.layout_keyboard_more_keys, null);
        mPopupWindow = new PopupWindow(container, -2, -2, false);
        mPopupWindow.setAnimationStyle(0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mPopupWindow.setElevation(mDensity * 3);
        }
        mRecyclerView = (RecyclerView) container.findViewById(R.id.rv_word);
        mMoreKeysAdapter = new MoreKeysAdapter(mContext, R.layout.item_more_keys, mPinYinCharacterList);
        mRecyclerView.setAdapter(mMoreKeysAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        mPopupWindow.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.bg_key_more_words));
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTouchX = motionEvent.getRawX();
                mPopupWindow.showAsDropDown(view, 0, (int) (-view.getHeight() * 2.3));
                break;
            case MotionEvent.ACTION_MOVE:
                float curX = motionEvent.getRawX();
                float deltaX = curX - mTouchX;
                int positionOffset = (int) (deltaX / (mDensity * 13));
                int position = /*mMoreKeysAdapter.getSelectedPosition() + */positionOffset;
                position = position <= 0 ? 0 : position >= mMoreKeysAdapter.getItemCount() ? mMoreKeysAdapter.getItemCount() - 1 : position;
                mMoreKeysAdapter.setSelectedPosition(position);
                mRecyclerView.scrollToPosition(position);
//                Log.e("tag------------", "" + deltaX / 30);
                break;
            case MotionEvent.ACTION_UP:
                if (mOnPinYinCharacterSelectedListener != null) {
                    mOnPinYinCharacterSelectedListener.onPinYinCharacterSelected(
                            mPinYinCharacterList.get(mMoreKeysAdapter.getSelectedPosition())
                            , mMoreKeysAdapter.getSelectedPosition());
                }
                mMoreKeysAdapter.setSelectedPosition(0);
                mRecyclerView.scrollToPosition(0);
                mPopupWindow.dismiss();
                break;
        }
        mLastX = motionEvent.getRawX();
        return true;
    }

    public interface OnPinYinCharacterSelectedListener {
        void onPinYinCharacterSelected(PinYinCharacter pinYinCharacter, int position);
    }
}